package com.cloud.custom.workflow.command.finish;

import java.util.Date;

import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import com.cloud.custom.common.context.HumanSession;
import com.cloud.custom.common.utils.SessionHelper;
import com.cloud.custom.workflow.command.Command;
import com.cloud.custom.workflow.command.WfRequest;
import com.cloud.custom.workflow.command.WfResponse;
import com.cloud.custom.workflow.entity.WfActInst;
import com.cloud.custom.workflow.entity.WfItemInst;
import com.cloud.custom.workflow.entity.WfPerPart;
import com.cloud.custom.workflow.enums.WfActStateEnum;
import com.cloud.custom.workflow.enums.WfItemTypeEnum;
import com.cloud.custom.workflow.enums.WfRecStateEnum;
import com.cloud.custom.workflow.mapper.WfActInstMapper;
import com.cloud.custom.workflow.mapper.WfItemInstMapper;
import com.cloud.custom.workflow.mapper.WfPerPartMapper;

/**
 * 案卷办结指令（考虑到目前业务量问题，暂时不做办结转移历史表操作）
 * 
 * @author taominglong
 *
 */
public class FinishCommand extends Command {

	@Override
	public void execute(WfRequest request, WfResponse resp, ApplicationContext context) {
		FinishRequest finishReq = (FinishRequest) request;
		HumanSession humanSession = SessionHelper.get();

		// 设置流程实例为已处理
		WfActInstMapper wfActInstMapper = context.getBean(WfActInstMapper.class);
		WfActInst actInst = wfActInstMapper.selectByPrimaryKey(finishReq.getActId());
		actInst.setActId(finishReq.getActId());
		actInst.setOpinion(finishReq.getOpinion());
		actInst.setActStateId(WfActStateEnum.PROCESSED.getActStateId());
		actInst.setActStateName(WfActStateEnum.PROCESSED.getActStateName());
		wfActInstMapper.updateByPrimaryKeySelective(actInst);

		// 工作项表
		WfItemInstMapper wfItemInstMapper = context.getBean(WfItemInstMapper.class);
		WfItemInst itemInst = new WfItemInst();
		itemInst.setActId(actInst.getActId());
		itemInst.setActDefName(actInst.getActDefName());
		itemInst.setActionTime(new Date());
		itemInst.setActionName("结案");
		itemInst.setDetail(humanSession.getHumanName() + "在" + actInst.getActDefName() + "阶段办理结案");
		itemInst.setItemContent(finishReq.getOpinion());
		itemInst.setHumanId(humanSession.getHumanId());
		itemInst.setHumanName(humanSession.getHumanName());
		itemInst.setItemTypeId(WfItemTypeEnum.FINISH.getItemTypeId());
		itemInst.setItemTypeName(WfItemTypeEnum.FINISH.getItemTypeName());
		wfItemInstMapper.insert(itemInst);

		// 经办人表
		WfPerPartMapper wfPerPartMapper = context.getBean(WfPerPartMapper.class);
		WfPerPart perPart = new WfPerPart();
		perPart.setHumanId(humanSession.getHumanId());
		perPart.setRecId(finishReq.getRecId());
		if (!wfPerPartMapper.existsWithPrimaryKey(perPart)) {
			wfPerPartMapper.insert(perPart);
		}

		// 设置案卷状态为办结
		JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
		jdbcTemplate.update("update rec_punish set state_id = ?, state_name = ? where rec_id = ?",
				WfRecStateEnum.FINISH.getStateId(), WfRecStateEnum.FINISH.getStateName(), finishReq.getRecId());
		
		FinishResponse finishResp = (FinishResponse)resp;
		finishResp.setRecId(finishReq.getRecId());
		finishReq.setActId(finishReq.getActId());
	}

}
